import 'package:flutter/material.dart';

class StarRating extends StatefulWidget {
  final int starCount;
  final double rating;
  final Color color;
  final double size;
  final Function(double rating)? onChange;

  StarRating({
    this.starCount = 5,
    this.rating = 0.0,
    this.color = Colors.amber,
    this.size = 25.0,
    this.onChange,
  });

  @override
  _StarRatingState createState() => _StarRatingState();
}

class _StarRatingState extends State<StarRating> {
  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: List.generate(widget.starCount, (index) {
        IconData iconData = Icons.star;
        Color color = widget.color;
        if (index >= widget.rating) {
          iconData = Icons.star_border;
          color = widget.color.withOpacity(0.5);
        } else if (index > widget.rating - 1 && index < widget.rating) {
          iconData = Icons.star_half;
        }

        return GestureDetector(
          onTap: () => widget.onChange?.call((index + 1).toDouble()),
          child: Icon(
            iconData,
            color: color,
            size: widget.size,
          ),
        );
      }),
    );
  }
}
